#ifndef OBJ3D_H
#define OBJ3D_H


#include "openGL/Angel.h"
#include "boundingbox.h"
#include <QThread>


#define OBJECT_MAX_RANGE 9999.0
#define OBJECT_MIN_RANGE -9999.0

struct Mesh {
    std::vector<GLushort>	elements;
    std::vector<vec4>		vertices;
    std::vector<vec3>		normals;
    std::vector<vec4>		face_vertices;
    std::vector<vec4>		flat_vertices;
    std::vector<vec3>		flat_normals;
};

class Obj3D : public QThread
{
public:
    Obj3D();
    void run();
    void loadFromFile();
    void loadFromFile(char*);

    void readObject();
    void readObject(
            const char* OBJFilePath,
            std::vector<vec4> &out_vertices,
            std::vector<vec4> &out_face_vertices,
            std::vector<vec3> &out_normals,
            std::vector<GLushort> &out_elements,
            std::vector<vec4> &flat_vertices,
            std::vector<vec3> &flat_normals);

    void setPosition(vec3 pos);
    vec3 getPosition();
    void translate(vec3 translation);
    BoundingBox getBoundingBox();
    bool testBoundingBoxIntersection(Obj3D* objectB);

    void initObjectM();
    void setObjectM();
    void setObjectM(mat4 modifiedM);



    void setObjName(char*);
    void setObjID(int);


    void clearMesh();
    void static_matrix(float rot[3],float trans[3],float scale[3]);


    void destroyVBO();

    void setTranslate(float[]);
    void setRotate(float[]);
    void setScale(float[]);
    void setRotateY(float a);

    void setTranslate(vec3);
    void setRotate(vec3);
    void setScale(vec3);


    void setDefaultTranslate(float[]);
    void setDefaultRotate(float[]);
    void setDefaultScale(float[]);

    void restore();

    void updateM();

    void drawBoundingBox();


    void readBound0();
    void readBound1();
    void readBound2();
    void readBound3();
    void readBound4();
    bool checkCollision(Obj3D* objB);

protected:
    vec3 m_pos;

    BoundingBox m_boundingBox;

    void constructBoundingVolume();


public :

    int size;

public :

    float rs[3];
    float ts[3];
    float ss[3];
    float default_rs[3];
    float default_ts[3];
    float default_ss[3];



    // Buffer ID
    uint	g_VBIDvertices;
    GLuint	g_VBIDnormals;
    GLuint	g_IBIDelements;


    // Buffer ID
    uint	bound_VBIDvertices[5];
    GLuint	bound_VBIDnormals[5];
    GLuint	bound_IBIDelements[5];

     int objID;
    Mesh obj;
    char* obj_name;
    mat4 objM;


    Mesh g_BVMesh;
    char* mesh_name;
    mat4 meshM;


    bool obj_animation;
    bool keyFrame_animation;

    double speedVector[3];

    double mass;

    double maxR;
    int numOfPoint;

    int boundID[5];
    Mesh bound[5];
    mat4 boundM[5];



};

#endif // OBJ3D_H
